package com.mxm.mirai.web;

import com.mxm.mirai.config.MXM_config;
import com.mxm.mirai.quartz.Quartz_AcgRandomImg_Run;
import com.mxm.mirai.quartz.Quartz_BiliBili_Run;
import com.mxm.mirai.quartz.Quartz_Weibo_Run;
import com.mxm.mirai.tools.BotLog;
import com.mxm.mirai.tools.QuartzTools;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@Api(value = "定时器 操作类")
public class QuartzWeb {
    //临时数据记录，随服务器关闭而清零，用于不需要持久化的开关记录
    public static Map<String,Object> datastate;
    private static BotLog log = new BotLog();
    static {
        datastate = new HashMap<>();
    }
    @GetMapping("/getQuartDataState")
    @RequiresRoles(logical = Logical.OR, value = {"admin"})
    @ApiOperation(value = "监控配置信息", notes = "监控配置信息--拦截")
    public Map getDataState(){
        datastate = new HashMap<>();
        datastate.put("BiliBili_on_off",!QuartzTools.notExists("BiliBili更新监控"));
        datastate.put("AcgRandomImg_on_off",!QuartzTools.notExists("AcgRandomImg"));
        datastate.put("WeiboMonitor_on_off",!QuartzTools.notExists("WeiboMonitor"));
        return datastate;
    }
    @GetMapping("/Quartz_BiliBili")
    @RequiresRoles(logical = Logical.OR, value = {"admin"})
    @ApiOperation(value = "BiliBili配置信息", notes = "BiliBili配置信息--拦截")
    public Map Quartz_BiliBili(){
        if(QuartzTools.notExists("BiliBili更新监控")){
            log.BotLog(null,"BiliBili【启动定时】开始(每30秒执行一次)...",true);
            QuartzTools.addJob("BiliBili更新监控",null, Quartz_BiliBili_Run.class, "30 * * * * ? *");
            datastate.put("BiliBili_on_off",1);
            return datastate;
        }else {
            log.BotLog(null,"BiliBili【移除定时】",true);
            QuartzTools.removeJob("BiliBili更新监控");
            datastate.put("BiliBili_on_off",0);
            return datastate;
        }
    }
    @GetMapping("/Quartz_AcgRandomImg/{time}")
    @RequiresRoles(logical = Logical.OR, value = {"admin"})
    @ApiOperation(value = "AcgRandomImg配置信息", notes = "AcgRandomImg配置信息--拦截")
    public Map Quartz_AcgRandomImg(@PathVariable(name="time") int time){
        if(QuartzTools.notExists("AcgRandomImg")){
            log.BotLog(null,"AcgRandomImg【启动定时】开始(每"+time+"分执行一次)...",true);
            QuartzTools.addJob("AcgRandomImg",null, Quartz_AcgRandomImg_Run.class, "0 /"+time+" * * * ? *");
            datastate.put("AcgRandomImg_on_off",1);
            return datastate;
        }else {
            log.BotLog(null,"AcgRandomImg【移除定时】",true);
            QuartzTools.removeJob("AcgRandomImg");
            datastate.put("AcgRandomImg_on_off",0);
            return datastate;
        }
    }
    @GetMapping("/Quartz_Weibo")
    @RequiresRoles(logical = Logical.OR, value = {"admin"})
    @ApiOperation(value = "Weibo配置信息", notes = "Weibo配置信息--拦截")
    public Map Quartz_tWeibo(){
        if(QuartzTools.notExists("WeiboMonitor")){
            log.BotLog(null,"WeiboMonitor【启动定时】开始(每3分执行一次)...",true);
            QuartzTools.addJob("WeiboMonitor",null, Quartz_Weibo_Run.class, "0 /3 * * * ? *");
            datastate.put("WeiboMonitor_on_off",1);
            return datastate;
        }else {
            log.BotLog(null,"WeiboMonitor【移除定时】",true);
            QuartzTools.removeJob("WeiboMonitor");
            datastate.put("WeiboMonitor_on_off",0);
            return datastate;
        }
    }
}
